<?php
/**
 * 优惠券控制器
 * @author lee
 */
namespace app\manage\controller;

use app\common\model\CouponGenerate;
use app\common\model\CouponReceive;
use app\common\model\Goods;
use app\common\model\MemberQy;
use app\facade\GoodCategory;
use app\common\model\CouponClass;
use app\common\model\Members as MemberModel;
class Coupon extends Base
{
	/**
	 * 添加/编辑优惠券
	 * @param array $info 编辑的信息
	 */
	public function addAttach($info)
	{

		$class = CouponClass::select();
        $this->assign("class",$class);
	}
	/*搜索商品信息*/
	public function goods()
	{
		$post = input('post.');
		if ($post['name']) {
			$name = $post['name'];
			$where[] = [
				['goods_name', 'like', '%'.$name.'%'],
			];
		}
		$goods = Goods::field('id,goods_name')->where($where)->order('id ASC')->select();
		echo json_encode($goods);
	}
    public function release(){
	    $id = input('id');
        $member = MemberModel::field('id,nickname')->select();
        $member_qy = MemberQy::field('id,name')->select();
	    return view('',[
	        'id' => $id,
            'member' => $member,
            'member_qy' => $member_qy
        ]);
    }
    public function release_coupon(){
	    $coupon_id = input('id');
	    $type = input('type');
	    $number = input('number');
	    $member_id = input('member_id');
	    $count_number = $number * count($member_id);
	    $coupon_count = CouponGenerate::where("is_release",0)->where('coupon_id',$coupon_id)->count('id');
	    if ($count_number > $coupon_count){
	        return j(1,'优惠劵不足');
        }
        $coupon = \app\common\model\Coupon::get($coupon_id);
	    foreach ($member_id as $key =>$value){
            //员工
	        if ($type == 1){
               CouponGenerate::where("is_release",0)->where('coupon_id',$coupon_id)->limit($number)->update([
                   'staff_id' => $value,
                   'type' => 1,
                   'is_release' => 1,
                   'release_time' => time(),
                   'pend' => $coupon['pend'],
               ]);
            }
	        //用户
            if ($type == 2){
                $coupon_generate_id = CouponGenerate::where("is_release",0)->where('coupon_id',$coupon_id)->limit($number)
                    ->field('id,coupon_id')->append(['pend'])->select();
                foreach ($coupon_generate_id as $kk => $vv) {
                    //更改优惠券状态
                    CouponGenerate::where("id",$vv['id'])->update([
                        'member_id' => $value,
                        'type' => 2,
                        'is_release' => 1,
                        'receive_status' => 1,
                        'release_time' => time(),
                        'pend' => $coupon['pend'],
                    ]);
                    //为用户添加优惠券
                    CouponReceive::create([
                        'coupon_generate_id' => $vv['id'],
                        'member_id' => $value,
                        'coupon_id' => $coupon_id,
                        'pend' => $vv['pend'],
                        'receive_time' => time(),
                    ]);
                }

            }

        }
	    $coupon->surplus_number = $coupon->surplus_number - $count_number;
	    $coupon->save();
        return j(0,'发劵成功');
    }
}
